package com.codedancer.campus.manager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.common.exception.BusinessException;
import com.codedancer.campus.common.utils.UUIDGenerator;
import com.codedancer.campus.manager.service.ISchoolService;
import com.codedancer.campus.manager.service.bo.school.SchoolAddOrUpdateBo;
import com.codedancer.campus.manager.service.bo.school.SchoolPageSearchBo;
import com.codedancer.campus.manager.service.vo.school.SchoolPageVo;
import com.codedancer.campus.manager.service.vo.school.SchoolVo;
import com.codedancer.campus.mapper.SchoolMapper;
import com.codedancer.campus.po.School;
import com.codedancer.campus.qo.school.SchoolPageSearchQo;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class SchoolServiceImpl implements ISchoolService {

    @Autowired
    private SchoolMapper schoolMapper;

    /**
     * 添加学校
     *
     * @param schoolAddOrUpdateBo
     */
    @Override
    public void add(SchoolAddOrUpdateBo schoolAddOrUpdateBo) {
        if (schoolMapper.existsByColumn("name", schoolAddOrUpdateBo.getName())) {
            throw new BusinessException("该学校名称已存在，不能添加");
        }
        School school = BeanUtil.copyProperties(schoolAddOrUpdateBo, School.class);
        school.setUuid(UUIDGenerator.getUUID());
        schoolMapper.insert(school);
    }

    /**
     * 修改学校
     *
     * @param schoolAddOrUpdateBo
     */
    @Override
    public void update(SchoolAddOrUpdateBo schoolAddOrUpdateBo) {
        School school = schoolMapper.findById(schoolAddOrUpdateBo.getUuid())
            .orElseThrow(() -> new BusinessException("学校不存在"));
        if (!school.getName().equals(schoolAddOrUpdateBo.getName())) {
            if (schoolMapper.existsByColumn("name", schoolAddOrUpdateBo.getName())) {
                throw new BusinessException("该学校名称已存在，不能添加");
            }
            school.setName(schoolAddOrUpdateBo.getName());
        }
        schoolMapper.updateById(school);
    }

    /**
     * 启用学校
     *
     * @param uuid
     */
    @Override
    public void enable(String uuid) {
        School school = schoolMapper.findById(uuid)
            .orElseThrow(() -> new BusinessException("学校不存在"));
        school.setDisabled(false);
        schoolMapper.updateById(school);
    }

    /**
     * 禁用学校
     *
     * @param uuid
     */
    @Override
    public void disable(String uuid) {
        School school = schoolMapper.findById(uuid)
            .orElseThrow(() -> new BusinessException("学校不存在"));
        school.setDisabled(true);
        schoolMapper.updateById(school);
    }

    /**
     * 分页查询学校
     *
     * @param schoolPageSearchBo
     * @return
     */
    @Override
    public IPage<SchoolPageVo> page(SchoolPageSearchBo schoolPageSearchBo) {
        SchoolPageSearchQo schoolPageSearchQo = BeanUtil.copyProperties(schoolPageSearchBo,
            SchoolPageSearchQo.class);
        IPage<School> schoolIPage = schoolMapper.page(
            new Page<School>(schoolPageSearchBo.getPage(), schoolPageSearchBo.getSize()),
            schoolPageSearchQo);
        IPage<SchoolPageVo> schoolPageVoIPage = new Page<>();
        List<SchoolPageVo> schoolPageVos;
        schoolPageVos = schoolIPage.getRecords().stream()
            .map(school -> BeanUtil.copyProperties(school, SchoolPageVo.class))
            .collect(Collectors.toList());
        schoolPageVoIPage.setTotal(schoolIPage.getTotal());
        schoolPageVoIPage.setRecords(schoolPageVos);
        return schoolPageVoIPage;
    }

    /**
     * 删除学校
     *
     * @param uuid
     */
    @Override
    public void delete(String uuid) {
        if (!schoolMapper.existsByUuId(uuid)) {
            throw new BusinessException("学校不存在");
        }
        schoolMapper.deleteById(uuid);
    }

    /**
     * 获取所有学校
     * @return
     */
    @Override
    public List<SchoolVo> all() {
        List<School> schools = schoolMapper.selectList(null);
        List<SchoolVo> schoolVos;
        schoolVos = schools.stream().map(school -> BeanUtil.copyProperties(school, SchoolVo.class))
            .collect(
                Collectors.toList());
        return schoolVos;
    }
}
